package utils

import (
	"fmt"
	"math/rand"
	"os"
	"strconv"
	"strings"
	"time"
)

var rander *rand.Rand

func init() {
	seed := os.Getpid()
	fmt.Printf("{utilsInit seed}|%d\n", seed)
	rander = rand.New(rand.NewSource(int64(seed)))
}

// 时间格式化模板
const TimeFormart string = "2006-01-02 15:04:05"

// 格式化时间
var formatMap = map[string]string{"YYYY": "2006", "YY": "06", "MM": "01", "DD": "02", "HH": "15", "mm": "04", "ss": "05"}

func GetFormatTimeStr(t time.Time, format string) string {
	realFormat := TimeFormart
	if len(format) > 0 {
		realFormat = format
		for k, v := range formatMap {
			realFormat = strings.ReplaceAll(realFormat, k, v)
		}
	}
	return t.Format(realFormat)
}

// 获取简单的uid 使用时间戳
func GetSimpleRandId() string {
	return GetFormatTimeStr(time.Now(), "YYMMDDHHmmss") + strconv.Itoa(rander.Intn(90)+10)
}

func ContainsString(src []string, dest string) bool {
	for _, s := range src {
		if s == dest {
			return true
		}
	}
	return false
}

// InterSectionStrings 获取两个字符串数组的交集
func InterSectionStrings(src []string, dest []string) []string {
	res := make([]string, 0)
	for _, s := range src {
		if ContainsString(dest, s) {
			res = append(res, s)
		}
	}
	return res
}
